import Dialog from '@vant/weapp/dialog/dialog';
import utils from '../../utils/util'
import {
  getRoom,
  delRoom
} from '../../api/myHouse'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    roomList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  goRouter(e) {
    let url = e.currentTarget.dataset.url
    let index = e.currentTarget.dataset.index
    if (url === '/pages/addRoom/addRoom' && this.data.roomList.length >= 5) {
      return wx.showToast({
        title: '添加房已达上限，请删除后添加',
        icon: 'none'
      })
    }
    utils.navigate(url, this.data.roomList[index])
  },
  del(val) {
    console.log(123);
    let that = this
    Dialog.confirm({
        context: this,
        message: '是否删除已绑定房屋',
      })
      .then(() => {
        wx.getStorage({
          key: 'token',
          async success(resToken) {
            let res = await delRoom({
              token: resToken.data,
              id: val.detail.id
            })
            if (res.data.code === 10000) {
              wx.showToast({
                title: '删除成功',
              })
              let arr = that.data.roomList
              arr.map((item, index) => {
                if (item.id === val.detail.id) {
                  arr.splice(index, 1)
                }
              })
              that.setData({
                roomList: arr
              })
            }
          }
        })
      }).catch(() => {});

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log(1232312312);
    let that = this
    wx.getStorage({
      key: 'token',
      success: async function (tokenRes) {
        await getRoom({
          token: tokenRes.data
        }).then(res => {
          console.log(res);
          if (res.data.code === 10000) {
            that.setData({
              roomList: res.data.data
            })
          }
        })
      },
      fail() {
        wx.redirectTo({
          url: '/pages/login/login',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})